<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

/**
 * App\Customer
 *
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer query()
 * @mixin \Eloquent
 * @property int $customer_id
 * @property string $name 客户名称
 * @property int $is_lock 1锁定
 * @property string $deal_status 成交状态
 * @property int $deal_time 领取，分配，创建时间
 * @property string|null $level 客户级别
 * @property string|null $industry 客户行业
 * @property string|null $source 客户来源
 * @property string|null $telephone 电话
 * @property string $mobile 手机
 * @property string $website 网址
 * @property string|null $remark 备注
 * @property int $create_user_id 创建人ID
 * @property int $owner_user_id 负责人ID
 * @property string $ro_user_id 只读权限
 * @property string $rw_user_id 读写权限
 * @property string $address 省市区
 * @property string $location 定位信息
 * @property string $detail_address 详细地址
 * @property float|null $lng 地理位置经度
 * @property float|null $lat 地理位置维度
 * @property int $next_time 下次联系时间
 * @property string|null $follow 跟进
 * @property int $create_time 创建时间
 * @property int $update_time 更新时间
 * @property int|null $project_id 项目id
 * @property string|null $status 线索状态
 * @property string|null $clue_id 来源id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereClueId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCreateUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCustomerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDealStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDealTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDetailAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereFollow($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereIndustry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereIsLock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereLocation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereNextTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereOwnerUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereProjectId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereRoUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereRwUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereUpdateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereWebsite($value)
 * @property string|null $crm_vfnlcv
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmVfnlcv($value)
 * @property-read \App\User|null $creater
 * @property-read \App\User|null $owner
 * @property string|null $chengjiaojine 成交金额
 * @property string|null $chengjiaodanwei 成交单位
 * @property string|null $daofang_time
 * @property string|null $chengjiaohangye 成交行业
 * @property string|null $chengjiaohetong 成交合同
 * @property string|null $chengjiaoqueren 成交确认
 * @property string|null $chengjiaoyongtu 成交用途
 * @property string|null $chengjiaomianji 成交面积
 * @property string|null $daofanghangye
 * @property string|null $daofangmianji
 * @property string|null $daofangqueren
 * @property string|null $daofangdanwei
 * @property string|null $baobei_time 报备
 * @property string|null $chengjiaoproject 成交项目
 * @property string|null $chengjiaokehuleixing 成交客户类型
 * @property string|null $chengjiaojiaokuanleixing 成交交款类型
 * @property-read \App\CustomerRemind|null $remindUser
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereBaobeiTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaodanwei($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaohangye($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaohetong($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaojiaokuanleixing($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaojine($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaokehuleixing($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaomianji($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaoproject($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaoqueren($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereChengjiaoyongtu($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDaofangTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDaofangdanwei($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDaofanghangye($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDaofangmianji($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereDaofangqueren($value)
 * @property-read \App\Project|null $project
 * @property int|null $is_sea 是否公海的
 * @property int|null $is_send_sea 是否发送过
 * @property int|null $sea_time 公海时间
 * @property int|null $big_part_id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereBigPartId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereIsSea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereIsSendSea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereSeaTime($value)
 * @property string|null $crm_uqdwei 客户反馈问题
 * @property string|null $crm_cuqaob 影响成交因素
 * @property string|null $crm_ocahkr 首次联系时间
 * @property string|null $crm_sfdbto 原厂区域
 * @property string|null $crm_oksvoe 邀约到访时间
 * @property int|null $crm_nwwouj 到访次数
 * @property-read \App\Record|null $record
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmCuqaob($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmNwwouj($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmOcahkr($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmOksvoe($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmSfdbto($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereCrmUqdwei($value)
 * @property int|null $is_send 发送
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Customer whereIsSend($value)
 */
class Customer extends Model
{
    protected $table = '5kcrm_crm_customer';
    protected $primaryKey = 'customer_id';
    public $timestamps = null;

    public function owner()
    {
        return $this->hasOne(User::class, 'id', 'owner_user_id');
    }

    public function record(){
        return $this->hasOne(Record::class,'types_id','customer_id')->where('types','crm_customer');
    }
    public function creater()
    {
        return $this->hasOne(User::class, 'id', 'create_user_id');
    }

    public function remindUser()
    {
        return $this->hasOne(CustomerRemind::class, 'customer_id', 'customer_id');
    }

    public function project()
    {
        return $this->hasOne(Project::class, 'project_id', 'project_id');
    }

    public static function fenpei($userIds)
    {

        $userfenshu = UserFenshu::whereIn('user_id', $userIds)->where('time', Carbon::now()->format('Y-m'))->get()->pluck('fenshu', 'user_id');
        $rand = [];
        $user = [];
        $num = 0;
        foreach ($userfenshu as $k => $item) {
            $user[] = [
                '用户名称' => User::find($k)->realname,
                '分数' => $item
            ];
            for ($i = 0; $i < $item; $i++) {
                array_push($rand, $k);
            }
            $num += $item;
        }
        if (empty($rand)) {
            $user_id = 1;
        } else {
            $user_id = $rand[array_rand($rand, 1)] ?? 1;

        }
       foreach ($userIds as $userId) {
            $userNum = UserNum::where('user_id', $userId)->first();
            if (!$userNum) {
                $userNum = new UserNum();
                $userNum->user_id = $userId;
                $userNum->num = 0;
                $userNum->save();
            }
            if ($userNum->num != 0 && $userNum->num % 5 == 0) {
                $user_id = $userNum->user_id;
                $userNum->num = $userNum->num + 1;
                $userNum->save();
                Log::channel('wechatLead')->info('五次必中', [$user_id]);
                break;
            }
        }

      foreach ($userIds as $userId) {
        $userNum = UserNum::where('user_id', $userId)->first();

        if (($user_id != $userNum->user_id&&$userNum->num % 5 != 0) ||($userNum->num==0&&$user_id != $userNum->user_id)) {
            $userNum->num = $userNum->num + 1;
            $userNum->save();
        }

    }

        $user[] = [
            '抽中人' => User::find($user_id)->realname
        ];
        Log::channel('wechatLead')->info('此次概率为', $user);
        return $user_id ?? 1;
    }
}
